import { Prisma, PrismaClient } from "@prisma/client";
import express from "express";

const prisma = new PrismaClient();
const router = express.Router();

router.get("/get", async function (req, res) {
  const { phoneNumber } = req.query;
  const AND: Prisma.PhoneWhereInput = phoneNumber
    ? {
        AND: [
          {
            phoneNumber: {
              equals: phoneNumber as string,
            },
          },
        ],
      }
    : {};
  const phones = await prisma.phone.findMany({
    where: {
      ...AND,
    },
  });

  return res.json({
    code: 0,
    data: phones,
  });
});
router.post("/add", async function (req, res) {
  const { phoneNumber } = req.body;
  if (!phoneNumber) {
    return res.json({
      code: -1,
      msg: "phoneNumber必填",
    });
  }
  try {
    await prisma.phone.create({
      data: {
        phoneNumber,
      },
    });
    return res.json({
      code: 0,
      data: [],
    });
  } catch (e) {
    return res.json({
      code: -1,
      msg: e,
    });
  }
});
export { router };
